Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: convert active rate to fraction to calculate opposite amount #5322

Merged
merged 2 commits into from
Jan 23, 2025

Conversation

shoom3301
Copy link
Collaborator

Summary

Fixes #5310

Sometimes activeRate is a Price and because of that output amount (for sell order) calculates wrongly.
I don't know why it is Price, in the most of cases it is a Fraction.

To Test

  1. Open Swap widget on Mainnet and set selling 1 WETH for USDC
  2. Go to Limit orders an wait till amount are calculates
  3. Changes sell amount to 2
  • AR: output amount is zero
  • ER: output amount corresponds to sell amount and price

@shoom3301 shoom3301 requested review from a team January 22, 2025 12:21
@shoom3301 shoom3301 self-assigned this Jan 22, 2025
Copy link

vercel bot commented Jan 22, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Updated (UTC)
cowfi ✅ Ready (Inspect) Visit Preview Jan 23, 2025 11:14am
explorer-dev ✅ Ready (Inspect) Visit Preview Jan 23, 2025 11:14am
swap-dev ✅ Ready (Inspect) Visit Preview Jan 23, 2025 11:14am
3 Skipped Deployments
Name Status Preview Updated (UTC)
cosmos ⬜️ Ignored (Inspect) Visit Preview Jan 23, 2025 11:14am
sdk-tools ⬜️ Ignored (Inspect) Visit Preview Jan 23, 2025 11:14am
widget-configurator ⬜️ Ignored (Inspect) Visit Preview Jan 23, 2025 11:14am

Copy link
Contributor

@elena-zh elena-zh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for addressing this!

)
const activeRateAsFraction = activeRate instanceof Price ? FractionUtils.fromPrice(activeRate) : activeRate
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The definition of Price is:

export declare class Price<TBase extends Currency, TQuote extends Currency> extends Fraction {

So a price is a valid fraction, why do we need specific handling?

Would be the problem that we use in the math the quotient instead of multiplying by numerator and dividing by denominator for the INPUT case, and the inverse of that for the OUTPUT case

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anxolin thanks for the question!
I've added a test with an explnation how it works:
7f7a81f

Please let me know if it doesn't answer on your question.

Copy link
Contributor

@anxolin anxolin Jan 28, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see.

I yout test, you multiply 1000000 weis of Ether (1e-12 Ether) by the price of ether --> 3208940687 weis of USDC for 1e18 weis of WETH --> 3208.940687 USDC/ETH

If you multiply you get 3208940687000000 / 1e18 ---> 0.003208940687

Because JSBI is a BigInt library, it will return 0 when you try to divide the numerator and denominator (quotient)

So maybe the issue, is we "expect" the price to be in units and it's in wei, but this is not how it stores the information inside. However, I believe it does the conversion you seek inside the Price implementation, IF ONLY it was a public method https://github.com/Uniswap/sdk-core/blob/main/src/entities/fractions/price.ts#L77

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell by their implementation of the Price, its correct. It makes sure you multiply using the right units. So here when you multiply weiWETH value for a price in weiUSD/weiWETH, you naturally get weiUSDC.

So I think multiplying directly should be actually safer. You just need to not use later the quotient without adjusting the units

Basically the function I share above (adjustedForDecimals) I think should convert a fraction with units of weiUSD/weiWETH into USD/WETH

Copy link
Collaborator

@alfetopito alfetopito left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great fix!

We use Price and Fraction instances interchangeably as if they are the same thing, but they are not.

It's great that we have the toPrice and fromPrice utils to help in cases like these.

Comment on lines +29 to +33
expect(value.numerator.toString()).toBe('1000000')
expect(value.denominator.toString()).toBe('1')

expect(price.numerator.toString()).toBe('3208940687')
expect(price.denominator.toString()).toBe('1000000000000000000')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's testing the Uniswap lib methods

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I know :)
This is a demo in order to answer on Anxos question

/**
* This is still valid fraction, and it does make sense, but let's see what we get bellow
*/
expect(multiplied.toFixed(10)).toBe('0.0032089407')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true that it doesn't work as expected because the Price instances are decimals aware.
That's why we shouldn't use multiply or prices as if they are regular fractions. We should use quote instead for them https://docs.uniswap.org/sdk/core/reference/classes/Price#quote

@alfetopito alfetopito merged commit 8afae8b into feat/limit-ui-upgrade Jan 23, 2025
12 of 13 checks passed
@alfetopito alfetopito deleted the fix/limit-price-zero branch January 23, 2025 13:46
@github-actions github-actions bot locked and limited conversation to collaborators Jan 23, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants